<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/18
 * Time: 17:30
 */

namespace Admin\Controller;


use Think\Controller;

class ZbController extends AdminController
{
    //对父类构造函数的补充
    public function _myinitialize()
    {
        $this->db = M('khzy');
        $this->db1 = M('dkzy');
        $this->db2 = M('cszy');
        $this->db3 = M('fkcb');
    }

    //http://www.glxt.com/admin/zb/lst
    //ajax得到总表的全部数据
    public function get_zb_data()
    {
        $where['status'] = array('gt', 0);
        $month = I('month');
        $year = I('year');
        if (!empty($month) && !empty($year)) { //所属月份
            $ssyf = $year . $month;
            $where['yf'] =$ssyf;
        }

        $khmc = I('khmc', '', 'trim');
        $yyslx = I('yyslx', '', 'trim');
        $ywqy = I('ywqy', '', 'trim');

        if (!empty($khmc)) $where['khmc'] = $khmc;
        if (!empty($yyslx)) $where['yyslx'] = $yyslx;
        if (!empty($ywqy)) $where['ywqy'] = $ywqy;

        if (count($where) === 1) { //此时查询条件仅为status=1，表示没有别的查询条件，则默认查询条件为：只显示上月的数据
            $last_m = date("Ym",strtotime(date("Ym01",time()))-8600);;//默认情况下显示上月的数据
            $where['yf'] = $last_m;
        }

        //先根据条件查出所有的客户信息
        $khzy_arr=$this->db->where($where)->select();
        $lsts = array();
        //按客户分组
        $khmc_arr = array_unique(array_column($khzy_arr, 'khmc'));
        foreach ($khzy_arr as $k => $v) {
            foreach ($khmc_arr as $v1) {
                if ($v['khmc'] === $v1) {
                    $lsts[$v1][] = $v;
                }
            }
        }
        unset($k, $v, $v1);
//        print_r($lsts);
        //根据每个客户的cszy_ids和dkzy_ids分别去两表中查出相关信息，根据客户名称去查
        $lists=array();
        foreach ($lsts as $k => $v) {

            $dkzy_ids = $v[0]['dkzy_ids'];
            $cszy_ids = $v[0]['cszy_ids'];

            $condition['id'] = array('in', $dkzy_ids);
            $map['id'] = array('in', $cszy_ids);
            unset($where['khmc'],$where['yyslx'],$where['ywqy']);
            $where['dykh']=$k;

            $dkzy_arr = $this->db1->where($condition)->select();
            $cszy_arr = $this->db2->where($map)->select();
            $fkcb_arr = $this->db3->where($where)->select();

            if (empty($dkzy_arr)) $dkzy_arr = array();
            if (empty($cszy_arr)) $cszy_arr = array();
            if (empty($fkcb_arr)) $fkcb_arr = array();

            $lists[$k]['khzy']=$v;
            $lists[$k]['dkzy'] = $dkzy_arr;
            $lists[$k]['cszy'] = $cszy_arr;
            $lists[$k]['fkcb'] = $fkcb_arr;
        }
        $this->ajaxReturn($lists);
    }

    public function lst(){
        $where['status'] = array('gt', 0);

        //所有客户名称列表
        $khmc_arr = $this->db->where($where)->getField('khmc', true);
        $khmc_arr = array_unique($khmc_arr);
        $this->assign('khmc_arr', $khmc_arr);

        //所有运营商类型列表
        $yyslx_arr = $this->db->where($where)->getField('yyslx', true);
        $yyslx_arr = array_unique($yyslx_arr);
        $this->assign('yyslx_arr', $yyslx_arr);

        //所有业务区域列表
        $ywqy_arr = $this->db->where($where)->getField('ywqy', true);
        $ywqy_arr = array_unique($ywqy_arr);
        $this->assign('ywqy_arr', $ywqy_arr);

        $this->display();
    }
}